package com.zmn.oms.task.job.orderworkingrecord;

import com.alibaba.fastjson.JSON;
import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.manager.lts.interfaces.task.TaskManager;
import com.zmn.oms.common.dro.orderworkingrecord.HistoryVoiceDetectionDRO;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.zmn.business.interfaces.orderworkingrecord.OrderWorkingRecordBService;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Map;

/**
 * 工程师录音任务
 *
 * @author chengguang
 * @date 2022/09/05
 */
@LTS
@Slf4j
public class OrderWorkingRecordJob {

    @Resource
    TaskManager taskManager;

    @Resource
    private OrderWorkingRecordBService orderWorkingRecordBService;


    @PostConstruct
    void init() {
//        Job job = new Job();
//        job.setTaskId(TaskConsts.TASK_ID_ORDER_RECORD_VOICE_DETECTION_JOB);
//
//        Map<String, String> extParam = new HashMap<>();
//        extParam.put("channelId", "123456");
//        extParam.put("cityId", "123456");
//        extParam.put("visitTime", "123456");
//        extParam.put("servCategId", "123456");
//        extParam.put("categId", "123456");
//        job.setExtParams(extParam);
//        job.setParam(TaskManager.SHARD_FIELD, TaskConsts.TASK_ID_ORDER_RECORD_VOICE_DETECTION_JOB);
////        job.setCronExpression("0 0 02 * * ?");
//        job.setReplaceOnExist(true);
//        ResponseDTO<String> result = taskManager.submitTask2(job);
//        if (!result.isSuccess()) {
//            log.error("#order_record_voice_detection_job# job submit fail...");
//        }
    }


    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_ORDER_RECORD_VOICE_DETECTION_JOB)
    public Result runJob(Job job) {

        try {
            Map<String, String> extParams = job.getExtParams();
            HistoryVoiceDetectionDRO historyVoiceDetectionDRO = JSON.parseObject(JSON.toJSONString(extParams), HistoryVoiceDetectionDRO.class);
            log.info("OrderWorkingRecordJob.runJob 入参{}", JSON.toJSONString(historyVoiceDetectionDRO));
            orderWorkingRecordBService.asyncHistoryVoiceDetection(historyVoiceDetectionDRO);
            return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
        } catch (Exception e) {
            log.info("OrderWorkingRecordJob.runJob 失败{}", e.getMessage(), e);
            return new Result(Action.EXECUTE_LATER, e.getMessage());
        }
    }

}
